<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../lib/vue-2.4.0.js"></script>
    <link rel="stylesheet" href="../lib/bootstrap-3.3.7.css">
    <style>
        table th {
            text-align: center
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">Panel title</h3>
            </div>
            <div class="panel-body form-inline">
                <label>id <input type="text" class="form-control" v-model="id"></label>
                <label>name <input type="text" class="form-control" v-model="name"></label>
                <label>price<input type="text" class="form-control" v-model="price"></label>

                <button type="button" class="btn  btn-primary" @click="add" @keyup.enter="add">添加</button>
                <label>搜索关键字 <input type="text" class="form-control" v-focus v-color="'green'"
                        v-model="keywords"></label>
            </div>

        </div>



        <table class="table table-hover table-bordered table-striped text-center">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Price</th>
                    <th>CreatTime</th>
                    <th>Operate</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item, index) in search(keywords)" :key="index">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.price|priceFormat}}</td>
                    <td>{{new Date()|dateFormat}}</td>
                    <td><a href="" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>



    </div>
    <script>
        var vm = new Vue({
            el: "#app",
            data() {
                return {
                    id: '',
                    name: '',
                    price: '',
                    keywords: '',
                    list: [
                        { id: 0, name: "奔驰", price: 100 },
                        { id: 1, name: "宝马", price: 200 },
                        { id: 2, name: "奥迪", price: 300 },
                        { id: 3, name: "奥迪", price: 400 },
                        { id: 4, name: "大众", price: 500 },
                    ]
                }
            },
            methods: {
                add() {
                    this.list.push({ id: this.id, name: this.name, price: this.price })
                    this.id=this.name=this.price=''
                },
                del(id) {
                    var index = this.list.findIndex(item => item.id == id)
                    this.list.splice(index, 1)
                },
                search(keywords) {
                  
                    return this.list.filter(item => item.name.includes(keywords))
                }
            },
            filters: {
                priceFormat(msg) {
                    // return '￥'+msg+'万元'
                    return `￥${msg}万元`
                },
                dateFormat(msg) {
                    var date = msg
                    var y = msg.getFullYear()
                    var m = (msg.getMonth() + 1).toString().padStart(2, '0')
                    var d = msg.getDate().toString().padStart(2, '0')
                    return `${y}-${m}-${d}`
                }
            },
            directives: {
                'focus': {
                    inserted(el, binding) {
                        el.focus()
                    }
                },
                'color': {
                    bind(el, binding) {
                        el.style.color = binding.value
                    }
                }
            }
        })
    </script>
</body>

</html>